package com.example.administrator.daydayappmarket.software.category.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.DownloadActivity;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.SearchActivity;
import com.example.administrator.daydayappmarket.software.choice.adapter.SoftwareChoiceListViewAdapter;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceTopicApps;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SoftwareCategorySecondActivity extends AppCompatActivity implements PullToRefreshBase.OnRefreshListener2 {

    @InjectView(R.id.activity_software_category_second_body_head_iamgeveiw0)
    ImageView activitySoftwareCategorySecondBodyHeadIamgeveiw0;
    @InjectView(R.id.activity_software_category_second_body_head_textview0)
    TextView activitySoftwareCategorySecondBodyHeadTextview0;
    @InjectView(R.id.activity_software_category_second_body_head_imagview2)
    ImageView activitySoftwareCategorySecondBodyHeadImagview2;
    @InjectView(R.id.activity_software_category_second_body_layout)
    RelativeLayout activitySoftwareCategorySecondBodyLayout;
    @InjectView(R.id.activity_software_category_second_listView)
    PullToRefreshListView activitySoftwareCategorySecondListView;
    @InjectView(R.id.activity_software_category_second)
    RelativeLayout activitySoftwareCategorySecond;
    @InjectView(R.id.activity_software_category_second_body_empty_image)
    ImageView activitySoftwareCategorySecondBodyEmptyImage;
    @InjectView(R.id.activity_software_category_second_body_emptyRelative)
    RelativeLayout activitySoftwareCategorySecondBodyEmptyRelative;
    @InjectView(R.id.activity_software_category_second_body_head_searchImage)
    ImageView activitySoftwareCategorySecondBodyHeadSearchImage;
    @InjectView(R.id.activity_software_category_second_body_head_backRelative)
    RelativeLayout activitySoftwareCategorySecondBodyHeadBackRelative;
    @InjectView(R.id.activity_software_category_second_body_head_downloadRelative)
    RelativeLayout activitySoftwareCategorySecondBodyHeadDownloadRelative;
    @InjectView(R.id.activity_software_category_second_body_head_searchRelative)
    RelativeLayout activitySoftwareCategorySecondBodyHeadSearchRelative;

    private List<SoftwareChoiceTopicApps> data;
    private SoftwareChoiceListViewAdapter adapter;
    private RequestQueue requestQueue;
    private int page = 1;
    private String id;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_software_category_second);
        ButterKnife.inject(this);

        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        title = intent.getStringExtra("title");
        data = new ArrayList<>();
        adapter = new SoftwareChoiceListViewAdapter(this, data);
        activitySoftwareCategorySecondListView.setAdapter(adapter);

        //设置空视图

        Glide.with(this).load(R.drawable.cat_loading_transparent).into(activitySoftwareCategorySecondBodyEmptyImage);
        activitySoftwareCategorySecondListView.setEmptyView(activitySoftwareCategorySecondBodyEmptyRelative);

        //设置上拉刷新和下拉刷新
        activitySoftwareCategorySecondListView.setMode(PullToRefreshBase.Mode.BOTH);
        activitySoftwareCategorySecondListView.setOnRefreshListener(this);


        requestQueue = Volley.newRequestQueue(this);
        refreshData(null);

        //设置左上角的返回监听
        activitySoftwareCategorySecondBodyHeadBackRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //设置右上角的下载跳转监听
        activitySoftwareCategorySecondBodyHeadDownloadRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(getApplicationContext(), DownloadActivity.class);
                startActivity(intent1);
            }
        });

        //设置右上角的查找跳转监听
        activitySoftwareCategorySecondBodyHeadSearchRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(getApplicationContext(), SearchActivity.class);
                startActivity(intent1);
            }
        });


    }

    private void refreshData(final PullToRefreshBase refreshView) {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Contacts.SOFTWARE_CATEGORY_SECOND_FRONT + id + Contacts.SOFTWARE_CATEGORY_SECOND_BELOW + page, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                if (response != null) {
                    try {
                        JSONArray jsonArray = new JSONArray(response);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                            SoftwareChoiceTopicApps softwareChoiceTopicApps = new SoftwareChoiceTopicApps();
                            softwareChoiceTopicApps.parseJson(jsonObject1);
                            data.add(softwareChoiceTopicApps);
                        }
                        adapter.notifyDataSetChanged();
                        activitySoftwareCategorySecondBodyHeadTextview0.setText(title);
                        if (refreshView != null) {
                            refreshView.onRefreshComplete();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        stringRequest.setTag("request");
        requestQueue.add(stringRequest);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        requestQueue.cancelAll("request");
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        data.clear();
        refreshData(refreshView);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        page++;
        refreshData(refreshView);

    }
}
